package cn.xorange.commons.system.controller.system;

import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.data.constants.DataConstants;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.*;
import cn.xorange.commons.system.service.System.ISysRoleService;
import cn.xorange.commons.core.constants.SysConstants;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author : yangjian
 * @date : 2022/2/22
 * @description :
 */
@RestController
@Authorize(hasPerm = "system:role")
@RequestMapping("/system/role")
public class SysRoleController extends DataController<SysRole, ISysRoleService> {


    @Override
    @Authorize(hasPerm = "query")
    @GetMapping("/{id}")
    public ResultBean info(@PathVariable(value = "id") Long id){

        SysRole role = this.service.getById(id);
        List<Long> menuIds = this.service.getRoleMenuIds(id);
        List<Long> orgIds = new ArrayList<>();
        if(DataConstants.DATA_SCOPE_CUSTOM.equals(role.getDataScope())){
            orgIds = this.service.getRoleOrgIds(id);
        }
        return ResultBean.onSuccess(role).put("menuIds",menuIds).put("orgIds",orgIds);
    }

    @Override
    @PostMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean add(@Validated @RequestBody SysRole sysRole){
        if(Objects.isNull(sysRole.getRoleKey()) || SecurityConstants.ADMIN_ROLE.equals(sysRole.getRoleKey())){
            return ResultBean.onFail("角色权限字符无效");
        }else {
            Long tenantId = SecurityUtils.getTenantId();
            sysRole.setTenantId(tenantId);
            int uniqueName = this.service.checkRoleNameUnique(sysRole);
            if(uniqueName>0){
                return ResultBean.onFail("存在相同角色名");
            }
            int uniqueKey = this.service.checkRoleKeyUnique(sysRole);
            if(uniqueKey>0){
                return ResultBean.onFail("存在相同权限字符");
            }
            this.service.add(sysRole);
            return ResultBean.onSuccess();
        }
    }

    public ResultBean update(@Validated @RequestBody SysRole sysRole){
        if(Objects.isNull(sysRole.getRoleKey()) || SecurityConstants.ADMIN_ROLE.equals(sysRole.getRoleKey())){
            return ResultBean.onFail("角色权限字符无效");
        }else {
            SysRole originRole = this.service.getById(sysRole.getRoleId());
            if(SysConstants.FLAG_NO.equals(originRole.getAllowed())){
                long userId = SecurityUtils.getUserId();
                boolean isAdmin = SecurityUtils.userIsAdmin();
                if(!isAdmin && userId != originRole.getCreateById()){
                    return ResultBean.onFail("请联系角色创建人进行操作");
                }
            }
            if(!originRole.getRoleName().equals(sysRole.getRoleName())){
                int uniqueName = this.service.checkRoleNameUnique(sysRole);
                if(uniqueName>0){
                    return ResultBean.onFail("存在相同角色名");
                }
            }
            if(!originRole.getRoleKey().equals(sysRole.getRoleKey())){
                int uniqueKey = this.service.checkRoleKeyUnique(sysRole);
                if(uniqueKey>0){
                    return ResultBean.onFail("存在相同权限字符");
                }
            }
            this.service.update(sysRole);
            return ResultBean.onSuccess();
        }
    }



}
